//cache缓存
import Cache from '@/utils/cache.js';
//用户
import userApi from '@/lib/user.js'
//学校
import schoolApi from '@/lib/school.js'
//项目
import subjectApi from '@/lib/Subject.js'
//用户
import Base from '@/common/base.js'
//语言
import Lang from '@/utils/lang.js';
//注册
const studentreg = '/pages/benefit/pages/studentreg';
//学生
const studentproject = '/pages/benefit/pages/studentproject';
//老师
const teacherproject = '/pages/benefit/pages/teacherproject';
//答题列表
const projectanswerend = '/pages/benefit/pages/projectanswerend';
//语文
const projectexercisestwo = '/pages/benefit/pages/projectexercisestwo';
//数学
const projectexercises = '/pages/benefit/pages/projectexercises';
//答题
const projectanswer = '/pages/benefit/pages/projectanswer';
//首页
const benefitindex = '/pages/benefit/pages/benefitindex';
//用户
const projectuser = '/pages/benefit/pages/projectuser';
//审核中心
const reviewapplication = '/pages/benefit/pages/reviewapplication';
//用户
const user = '/pages/benefit/pages/user';
//审核申请
const reviewapadd = '/pages/benefit/pages/reviewapadd';
//添加班级
const reviewapgradeadd = '/pages/benefit/pages/reviewapgradeadd';
//高效阅读
const projectlist = '/pages/benefit/pages/projectlist';
//我的激活
const mycode = '/pages/benefit/pages/mycode';
//我的激活列表
const mycodelist = '/pages/benefit/pages/mycodelist';


//自动url 
const automaticurl = function(data) {
	if (data == 'mycodelist') {
		return mycodelist;
	}
	if (data == 'mycode') {
		return mycode;
	}
	if (data == 'studentreg') {
		return studentreg;
	}
	if (data == 'projectlist') {
		return projectlist;
	}
	if (data == 'studentproject') {
		return studentproject;
	}
	if (data == 'projectanswer') {
		return projectanswer;
	}
	if (data == 'projectanswerend') {
		return projectanswerend;
	}
	if (data == 'projectexercises') {
		return projectexercises;
	}
	if (data == 'projectexercisestwo') {
		return projectexercisestwo;
	}
	if (data == 'teacherproject') {
		return teacherproject;
	}
	if (data == 'benefitindex') {
		return benefitindex;
	}
	if (data == 'projectuser') {
		return projectuser;
	}
	if (data == 'reviewapplication') {
		return reviewapplication;
	}
	if (data == 'user') {
		return user;
	}
	if (data == 'reviewapadd') {
		return reviewapadd;
	}
	if (data == 'reviewapgradeadd') {
		return reviewapgradeadd;
	}
}
//错误提示
const homeUrl = async function() {
	Base.publicpopup(Lang.lang.infoError, Lang.lang.authorisationerrordesc);
	return false;
}
//判断是否存在1
const ifStudenter = async function() {
	//判断是否登录
	var backmsg = await userApi.getStudenterApi();
	if (backmsg.data.success) {
		//学生信息
		if (!backmsg.data.data.team_class) {
			return true; //没有学生信息不跳转
		} else {
		
			Cache.set('userid', backmsg.data.data.user_id);
			
			if (backmsg.data.data.activation != 1) {
				//判断是否已经激活
				if (!backmsg.data.data.activation_time) {
					Base.publicpopup('提示', '未激活账户,请激活.', automaticurl('mycode'));
					return;
				}
				//判断时间是否到期
				if (backmsg.data.data.activation_time) {
					var nowtime = parseInt(Date.parse(new Date())) / 1000;
					var activation_time=parseInt(Base.timeProcessing(backmsg.data.data.activation_time))/1000;
					if (nowtime > activation_time) {
						console.log(Base.timeProcessing(backmsg.data.data.activation_time));
						Base.publicpopup('提示', '激活已到期，请重新激活.', automaticurl('mycode'));
						return;
					}else{
						console.log("已激活");
					}
				}
			}
			Cache.set('activation_time', backmsg.data.data.activation_time);
			Cache.set('activation', backmsg.data.data.activation); //1=永久激活
			Cache.set('username', backmsg.data.data.username);
			//存在保存学生信息
			Cache.set('teamid', backmsg.data.data.team);
			Cache.set('teamclass', backmsg.data.data.team_class);
			Cache.set('gradeid', backmsg.data.data.grade_id);
			Cache.set('schoolid', backmsg.data.data.school_id);
			Cache.set('isteam', backmsg.data.data.is_team);
			return false; //有学生信息
		}
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}

}

//判断是否存在
const ifTeacher = async function() {
	//判断是否登录
	if (Cache.has('user_openid') && !Cache.has('teacherteam')) {
		var backmsg = await userApi.getTeacherApi();
		if (backmsg.data.success) {
			//老师信息
			if (!backmsg.data.data.teacher_team) {
				return true; //没有老师信息
			} else {
				//存在保存老师信息
				Cache.set('teacherteam', backmsg.data.data.teacher_team);
				Cache.set('userid', backmsg.data.data.user_id);
				Cache.set('schoolid', backmsg.data.data.school_id);
				return false; //有老师信息
			}
		} else {
			//错误提示并跳转首页
			this.homeUrl();
		}
	}
}

//添加用户管理班级列表
const setUserTeamApiMethod = async function(data) {
	var backmsg = await userApi.setUserTeamApi(data);
	if (backmsg.data.success) {
		if (backmsg.data.data.code == '1') {
			//成功更新
			Base.publicshowtoastsuccess(Lang.lang.success, reviewapgradeadd);
		} else {
			//失败更新 
			Base.publicshowtoasterror(Lang.lang.error);
		}
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}
//填写学生信息
const InFormationFillingApiMethod = async function(data) {
	var backmsg = await userApi.inFormationFillingApi(data);
	if (backmsg.data.success) {
		if (backmsg.data.data.code == '1') {
			//成功更新
			Base.publicshowtoastsuccess(Lang.lang.success, user);
		} else {
			//失败更新 
			Base.publicshowtoasterror(Lang.lang.error);
		}
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}
//获取学校
const getSchoolApiMethod = async function(data) {
	var backmsg = await schoolApi.getSchoolApi(data);
	if (backmsg.data.success) {
		if (backmsg.data.data.code == '1') {
			//成功
			return backmsg.data.data.data;
		} else {
			//失败
			Base.publicshowtoasterror(Lang.lang.error);
		}
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}
//获取学校列表
const getSchoolListApiMethod = async function() {
	var backmsg = await schoolApi.getSchoolListApi();
	if (backmsg.data.success) {
		if (backmsg.data.data.code == '1') {
			//成功
			return backmsg.data.data.data;
		} else {
			//失败
			Base.publicshowtoasterror(Lang.lang.error);
		}
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}
//获取学生列表
const getUserListApiMethod = async function(data) {
	var backmsg = await userApi.getUserListApi(data);
	if (backmsg.data.success) {
		if (backmsg.data.data.code == '1') {
			//成功
			return backmsg.data.data.data;
		} else {
			//失败
			Base.publicshowtoasterror(Lang.lang.error);
		}
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}
//获取学校列表
const getTeamListApiMethod = async function(data) {
	var backmsg = await schoolApi.getTeamListApi(data);
	if (backmsg.data.success) {
		if (backmsg.data.data.code == '1') {
			//成功
			return backmsg.data.data.data;
		} else {
			//失败
			Base.publicshowtoasterror(Lang.lang.error);
		}
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}

//获取班级对应的配置
const getTeamGradeOptionApiMethod = async function(data = {}) {
	var backmsg = await subjectApi.getTeamGradeOptionApi(data);
	if (backmsg.data.success) {
		//成功
		return backmsg.data.data.option;
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}


//随机获取题
const getSubjectListApiMethod = async function(data = {}) {
	var backmsg = await subjectApi.getSubjectListApi(data);
	if (backmsg.data.success) {
		if (backmsg.data.data.code == '1') {
			//成功
			return backmsg.data.data.data;
		} else {
			//失败
			Base.publicshowtoasterror(Lang.lang.error);
		}
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}
//得到科目信息
const getRandomProjectApiMethod = async function(data = {}) {
	var backmsg = await subjectApi.getRandomProjectApi(data);
	if (backmsg.data.success) {
		if (backmsg.data.data.code == '1') {
			//成功
			return backmsg.data.data.data;
		} else {
			//失败
			Base.publicshowtoasterror(Lang.lang.error);
		}
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}
//得到科目信息
const getSubjectApiMethod = async function(data = {}) {
	var backmsg = await subjectApi.getSubjectApi(data);
	if (backmsg.data.success) {
		if (backmsg.data.data.code == '1') {
			//成功
			return backmsg.data.data.data;
		} else {
			//失败
			Base.publicshowtoasterror(Lang.lang.error);
		}
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}
//得到项目列表
const getProjectListApiMethod = async function(data = {}) {
	var backmsg = await subjectApi.getProjectListApi(data);
	if (backmsg.data.success) {
		if (backmsg.data.data.code == '1') {
			//成功
			return backmsg.data.data.data;
		} else {
			//失败
			Base.publicshowtoasterror(Lang.lang.error);
		}
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}
//得到项目信息
const getProjectApiMethod = async function(data = {}) {
	var backmsg = await subjectApi.getProjectApi(data);
	if (backmsg.data.success) {
		if (backmsg.data.data.code == '1') {
			//成功
			return backmsg.data.data.data;
		} else {
			//失败
			Base.publicshowtoasterror(Lang.lang.error);
		}
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}
//提交答案
const setUserProjectApiMethod = async function(data = {}) {
	var backmsg = await subjectApi.setUserProjectApi(data);
	if (backmsg.data.success) {
		if (backmsg.data.data.code == '1') {
			//成功
			return backmsg.data.data.data;
		} else {
			//失败
			Base.publicshowtoasterror(Lang.lang.error);
		}
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}
//得到项目结果
const getUserHistoryProjectApiMethod = async function(data = {}) {
	var backmsg = await subjectApi.getUserHistoryProjectApi(data);
	if (backmsg.data.success) {
		if (backmsg.data.data.code == '1') {
			//成功
			return backmsg.data.data.data;
		} else {
			//失败
			Base.publicshowtoasterror(Lang.lang.error);
		}
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}
//答题项目历史列表
const getUserHistoryProjectListApiMethod = async function(data) {
	var backmsg = await subjectApi.getUserHistoryProjectListApi(data);
	if (backmsg.data.success) {
		if (backmsg.data.data.code == '1') {
			//成功
			return backmsg.data.data.data;
		} else {
			//失败
			Base.publicshowtoasterror(Lang.lang.error);
		}
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}
//解析微信小程序获得的手机号
const getUserPhoneApiMethod = async function(data = {}) {
	var backmsg = await userApi.getUserPhoneApi(data);
	if (backmsg.data.success) {
		if (backmsg.data.data.code == '1') {
			//成功
			return backmsg.data.data.data;
		} else {
			//失败
			Base.publicshowtoasterror(Lang.lang.reclickBind);
		}
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}
//活码激活日志
const getWelfareCdkeyLogApiMethod = async function(data = {}) {
	var backmsg = await userApi.getWelfareCdkeyLogApi(data);
	if (backmsg.data.success) {
		return backmsg.data.data;
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}


//学生激活
const StudentActivationApiMethod = async function(data = {}) {
	var backmsg = await userApi.StudentActivationApi(data);
	if (backmsg.data.success) {
		return backmsg.data.data;
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}


//获取用户信息
const getUserApiMethod = async function(data = {}) {
	var backmsg = await userApi.getUserApi(data);
	if (backmsg.data.success) {
		return backmsg.data.data;
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}
//获取等级
const getGradeListApiMethod = async function(data = {}) {
	var backmsg = await subjectApi.getGradeListApi(data);
	if (backmsg.data.success) {
		return backmsg.data.data;
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}
//申请查看训练者信息
const addUserManageApiMethod = async function(data = {}) {
	var backmsg = await subjectApi.addUserManageApi(data);
	if (backmsg.data.success) {
		if (backmsg.data.data.code == '1') {
			//成功
			return backmsg.data.data;
		} else {
			//失败
			Base.publicshowtoasterror(Lang.lang.error);
		}
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}
//申请查看训练者信息列表
const getUserManageListApiMethod = async function(data = {}) {
	var backmsg = await subjectApi.getUserManageListApi(data);
	if (backmsg.data.success) {
		if (backmsg.data.data.code == '1') {
			//成功
			return backmsg.data.data;
		} else {
			//失败
			Base.publicshowtoasterror(Lang.lang.error);
		}
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}
//训练者查看申请信息
const getUserManageApiMethod = async function(data = {}) {
	var backmsg = await subjectApi.getUserManageApi(data);
	if (backmsg.data.success) {
		return backmsg.data.data;
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}
//训练者确认申请者查看
const setUserManageApiMethod = async function(data = {}) {
	var backmsg = await subjectApi.setUserManageApi(data);
	if (backmsg.data.success) {
		if (backmsg.data.data.code == '1') {
			//成功
			return backmsg.data.data;
		} else {
			//失败
			Base.publicshowtoasterror(Lang.lang.error);
		}
	} else {
		//错误提示并跳转首页
		this.homeUrl();
	}
}

export default {
	getWelfareCdkeyLogApiMethod,
	StudentActivationApiMethod,
	getRandomProjectApiMethod,
	ifStudenter,
	getSubjectApiMethod,
	InFormationFillingApiMethod,
	homeUrl,
	getSchoolListApiMethod,
	getTeamListApiMethod,
	getSubjectListApiMethod,
	getProjectListApiMethod,
	getSchoolApiMethod,
	getProjectApiMethod,
	automaticurl,
	setUserProjectApiMethod,
	getUserHistoryProjectApiMethod,
	getUserHistoryProjectListApiMethod,
	getUserPhoneApiMethod,
	ifTeacher,
	getUserListApiMethod,
	getUserApiMethod,
	getGradeListApiMethod,
	addUserManageApiMethod,
	getUserManageListApiMethod,
	getUserManageApiMethod,
	setUserManageApiMethod,
	setUserTeamApiMethod,
	getTeamGradeOptionApiMethod,
}